<?php
class AjaxController extends AppController{
    public function newsletter()
    {
        $this->autoRender = false;
        $email = @$_POST['email'];
        $gender = @$_POST['sex'];
        
        $this->loadModel('Newsletter');
        $check = $this->Newsletter->check_email($email);
        if($check)
        {
            $check = $this->Newsletter->add_newsletter($email, $gender);
            echo '1';
        }
        else
            echo '0';
    }
    
    public function order()
    {
        $this->autoRender = false;
        
        $this->loadModel('Product');
        $product_id = $_POST['product_id'];
        $info_product = $this->Product->get_info_product('cutkit', $product_id);
        if(!empty($info_product))
        {
            $carts = array();
            if($this->Session->read('carts'))
                $carts = $this->Session->read('carts');
            
            if(isset($carts[$product_id]['number']))
                $carts[$product_id]['number']++;
            else
                $carts[$product_id]['number'] = 1;
            $carts[$product_id]['price'] = $info_product['0']['product']['price'];
            $carts[$product_id]['discount'] = $info_product['0']['product']['discount'];
            $carts[$product_id]['percent_discount'] = $info_product['0']['product']['percent_discount'];
            $carts[$product_id]['title'] = $info_product['0']['product_desc']['title'];
            $carts[$product_id]['image'] = $info_product['0']['image']['name'];
            $carts[$product_id]['in_stock'] = $info_product['0']['product']['in_stock'];
            
            $this->Session->write('carts',$carts);
            $this->Session->setFlash('Sản phẩm <strong>'.$info_product['0']['product_desc']['title'].'</strong> đã thêm vào giỏ hàng thành công.');
        }
    }
    
    public function delete_cart()
    {
        $this->autoRender = false;
        
        $this->loadModel('Product');
        $product_id = $_POST['product_id'];
        $info_product = $this->Product->get_info_product('cutkit', $product_id);
        
        $carts = $this->Session->read('carts');
        $title = $carts[$product_id]['title'];
        unset($carts[$product_id]);
        $this->Session->write('carts',$carts);
        $this->Session->setFlash('Sản phẩm <strong>'. $title.'</strong> đã xóa khỏi giỏ hàng thành công.');
    }
    
    public function change_cart()
    {
        $this->autoRender = false;
        
        $this->loadModel('Product');
        $product_id = $_POST['product_id'];
        $number = $_POST['number'];

        $carts = $this->Session->read('carts');
        $title = $carts[$product_id]['title'];
        $carts[$product_id]['number'] = $number;
        $this->Session->write('carts',$carts);
        $this->Session->setFlash('Sản phẩm <strong>'. $title.'</strong> đã cập nhật số lượng thành công.');
    }
    
    public function favorite()
    {
        $this->autoRender = false;
        
        $this->loadModel('Product');
        $product_id = $_POST['product_id'];
        $type = @$_POST['type'];
        
        $favorite = array();
            if($this->Cookie->read('favorite'))
                $favorite = $this->Cookie->read('favorite');
                
        if(isset($favorite[$product_id])){
            echo '0';
            exit;
        }
        $type != '' ? $type : 'cutkit';  
        $info_product = $this->Product->get_info_product($type, $product_id);
        if(!empty($info_product))
        {
            $favorite[$product_id]['price'] = $info_product['0']['product']['price'];
            $favorite[$product_id]['discount'] = $info_product['0']['product']['discount'];
            $favorite[$product_id]['percent_discount'] = $info_product['0']['product']['percent_discount'];
            $favorite[$product_id]['title'] = $info_product['0']['product_desc']['title'];
            $favorite[$product_id]['image'] = $info_product['0']['image']['name'];
            $favorite[$product_id]['in_stock'] = $info_product['0']['product']['in_stock'];
            $favorite[$product_id]['link'] = $info_product['0']['link']['link'];
            $favorite[$product_id]['type'] = $info_product['0']['product']['type'];
            $this->Cookie->write('favorite',$favorite, false, 3600);
            echo 1;
            //$this->Session->setFlash('Sản phẩm <strong>'.$info_product['0']['product_desc']['title'].'</strong> đã thêm vào giỏ hàng thành công.');
        }
    }
    
    public function rating()
    {
        $this->autoRender = false;
        $this->loadModel('Product');
        
        $product_id = $_POST['product_id'];
        $rating_detail = $_POST['rating_detail'];
        $info_product = $this->Product->get_info_product('', $product_id);
        $rating = @$info_product[0]['product']['rating'] == '' ? 0 : $info_product[0]['product']['rating'];
        $count_rating = @$info_product[0]['product']['count_rating'];
        $count_rating ++;
        $rating_change = $rating == 0 ? $rating_detail : number_format(($rating + $rating_detail)/2,1);
        
        $sql = "UPDATE product SET rating = '$rating_change', count_rating = '$count_rating' WHERE product_id = $product_id";
        $this->Product->update_rating($sql);
    }
    
    public function valid_email()
    {
        $this->autoRender = false;
        $email = $_POST['email'];
        $this->loadModel('User');
        $sql = array('conditions' => array('email =' => $email));
        $data = $this->User->find('all', $sql);
        if(!empty($data))
        	echo 'false';
        else
        	echo 'true';
    }
    
    public function valid_username()
    {
        $this->autoRender = false;
        $username = $_POST['username'];
        $sql = array('conditions' => array('username =' => $username));
        $this->loadModel('User');
        $data = $this->User->find('all', $sql);
        if(!empty($data))
        	echo 'false';
        else
        	echo 'true';   
    }
    
    public function change_lang_code()
    {
        $this->autoRender = false;
        $lang_code = $_POST['lang_code'];
        $this->Session->write('lang_code', $lang_code);
    }
}